package com.scqkxx.gamebox.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.myself.LoginActivity;
import com.scqkxx.gamebox.bean.BannerBean;
import com.scqkxx.gamebox.utils.Utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.bingoogolapple.bgabanner.BGABanner;


/**
 * 开机导航页
 * Created by heli on 2016-09-27.
 */

public class WelComeActivity extends Activity implements View.OnClickListener {
    private Button mEnterBtn;
    private BGABanner mBackgroundBanner;
    private int pageSize = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT);
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        initView();
        setListener();
        processLogic();
    }

    private void initView() {
        mEnterBtn = (Button) findViewById(R.id.btn_guide_enter);
        mBackgroundBanner = (BGABanner) findViewById(R.id.banner_guide_background);
    }

    private void setListener() {
        mEnterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(WelComeActivity.this, MainActivity.class));
                finish();
            }
        });

        // 监听页码切换事件，控制「跳过按钮」和「进入主界面的显示与隐藏」
//        mBackgroundBanner.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                pageSize = position;
//                if (position == mBackgroundBanner.getItemCount() - 2) {
//                    ViewCompat.setAlpha(mEnterBtn, positionOffset);
//
//                    if (positionOffset > 0.5f) {
//                        mEnterBtn.setVisibility(View.VISIBLE);
//                    } else {
//                        mEnterBtn.setVisibility(View.GONE);
//                    }
//                } else if (position == mBackgroundBanner.getItemCount() - 1) {
//                    mEnterBtn.setVisibility(View.VISIBLE);
//                    ViewCompat.setAlpha(mEnterBtn, 1.0f);
//                } else {
//                    mEnterBtn.setVisibility(View.GONE);
//                }
//            }
//        });

//        mBackgroundBanner.setDelegate(new BGABanner.Delegate() {
//            @Override
//            public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {
//                if (pageSize == 5) {
//                    startActivity(new Intent(WelComeActivity.this, LoginActivity.class));
//                    finish();
//                } else {
//                    pageSize++;
//                    mBackgroundBanner.setCurrentItem(pageSize);
//                }
//            }
//        });
        mBackgroundBanner.setOnTouchListener((v, event) -> false);
    }

    private void processLogic() {
        List<BannerBean> list = new ArrayList();
        list.add(new BannerBean(R.mipmap.ic_launcher ,"欢迎" ,"开始体验" ,"感谢使用实物！接下来的几张卡片 会教您基本功能，滑动卡片或者点击下 方的按钮即可开始体验"));
        list.add(new BannerBean(R.mipmap.ic_launcher ,"标识物品收纳助手" ,"简单易用的物品收纳助手" ,"您可以使用实物将物品的位置进行 标记，助您完成井井有条的物品收纳整 理从此告别丢三落四！"));
        list.add(new BannerBean(R.mipmap.ic_launcher ,"物品变动提醒" ,"随时查看物品最新情况" ,"实物会及时通知您添加的物品信息变动"));
        list.add(new BannerBean(R.mipmap.ic_launcher ,"群组" ,"建立群组" ,"您可以邀请您的家人/朋友来对公共空间进行协调管理，同时这些信息在群组成员之间都是共享的"));
        mBackgroundBanner.setAutoPlayAble(false);//是否自动轮播
        mBackgroundBanner.setData(R.layout.banner_layout,list ,null);
        mBackgroundBanner.setAdapter((BGABanner.Adapter<LinearLayout, BannerBean>) (banner, itemView, model, position) -> {
            //图片布局
            ImageView logo = itemView.findViewById(R.id.logo);
            TextView title = itemView.findViewById(R.id.title);
            TextView titleName = itemView.findViewById(R.id.titleName);
            TextView titleContent = itemView.findViewById(R.id.titleContent);
            TextView tv_confirm = itemView.findViewById(R.id.tv_confirm);
            logo.setImageResource(model.getImageSrc());
            title.setText(model.getTitle());
            titleName.setText(model.getTitleName());
            titleContent.setText(model.getTitleContent());
            tv_confirm.setOnClickListener(v -> {
                if (position  == 3 ){
                    startActivity(new Intent(WelComeActivity.this, LoginActivity.class));
                    finish();
                } else {
                    int i = position;
                    i++;
                    Log.e("TAG" , i+"");
                    mBackgroundBanner.setCurrentItem(i);
                }
            });
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_guide_enter) {

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 在界面可见时给背景Banner设置一个白色背景，避免滑动过程中两个Banner都设置透明度后能看到Launcher
        mBackgroundBanner.setBackgroundResource(android.R.color.white);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    /**
     * 图片内存溢出处理
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        opt.inSampleSize = Utils.computeSampleSize(opt, -1, 128 * 128);
        opt.inJustDecodeBounds = false;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);

        return BitmapFactory.decodeStream(is, null, opt);
    }
}